package com.woniu.controller;

import com.woniu.entity.Menu;
import com.woniu.entity.User;
import com.woniu.entity.response.ResponseEntity;
import com.woniu.security.JwtUtil;
import com.woniu.service.IMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brain/menu")
@Slf4j
@CrossOrigin(allowCredentials = "true", originPatterns = "*")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @Autowired
    private JwtUtil jwtUtil;

    @GetMapping(value = "/list", produces = "application/json;charset=UTF-8")
    public ResponseEntity<List<Menu>> list(User user) {
        return ResponseEntity.success(menuService.getByRoleId(user.getRole().getRoleId()));
    }

    @GetMapping("indexMenus")
    public ResponseEntity<List<Menu>> getByRoleId(@RequestHeader String token) {
        int roleId = (int) jwtUtil.getInfo(token, "roleId");

        return ResponseEntity.success( menuService.getByRoleId(roleId));


    }
}
